//
//  LSWaittingHandleListViewController.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2019/1/23.
//  Copyright © 2019 wpkj. All rights reserved.
//

#import "LSWaittingHandleListViewController.h"
#import "LSAttendanceManagementTableViewCell.h"
#import "LSAttendanceRecordsDetailViewController.h"

#define CountPerPage 10

@interface LSWaittingHandleListViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) int tableViewPage;
@property (nonatomic, strong) NSMutableArray *dataArrayM;
@property (nonatomic, strong) NZKNoDataView *nodataView;
@property (nonatomic, strong) NZKNetworkErrorView *networkErrorView;
@end

static NSString *LSAttendanceManagementTableViewCellID = @"LSAttendanceManagementTableViewCellID";

@implementation LSWaittingHandleListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"待办列表";
    [self loadData];
    [self.view addSubview:self.tableView];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
}

- (void)loadData {
    self.tableViewPage = 1;
    NSDictionary *param = @{@"token":[ValidateUtils validateNull:[UserInfoModel sharedInstance].token],@"rows":[NSString stringWithFormat:@"%d",CountPerPage],@"page":[NSString stringWithFormat:@"%d",self.tableViewPage]};
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:WaittingHandleList param:param completionHandler:^(id responseObject, NSError *connectionError) {
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        [self.tableView.mj_header endRefreshing];
        if (!connectionError) {
            NSArray *dataArray = [responseObject objectForKey:@"data"];
            if (dataArray && dataArray.count > 0) {
                [self.nodataView removeFromSuperview];
                [self.networkErrorView removeFromSuperview];
                self.dataArrayM = [[NSMutableArray alloc] initWithArray:dataArray];
                if (self.dataArrayM.count < self.tableViewPage*CountPerPage) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }
                [self.tableView reloadData];
            } else {
                [self.dataArrayM removeAllObjects];
                [self.tableView reloadData];
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
                [self.networkErrorView removeFromSuperview];
                [self.tableView addSubview:self.nodataView];
            }
        } else {
            [self.nodataView removeFromSuperview];
            [self.tableView addSubview:self.networkErrorView];
            self.networkErrorView.tipText = [NSString stringWithFormat:@"%@",connectionError.localizedDescription];
        }
    }];
}

- (void)loadMoreData {
    self.tableViewPage++;
    NSDictionary *param = @{@"token":[ValidateUtils validateNull:[UserInfoModel sharedInstance].token],@"rows":[NSString stringWithFormat:@"%d",CountPerPage],@"page":[NSString stringWithFormat:@"%d",self.tableViewPage]};
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:WaittingHandleList param:param completionHandler:^(id responseObject, NSError *connectionError) {
        if (!connectionError) {
            NSArray *dataArray = [responseObject objectForKey:@"data"];
            if (dataArray && dataArray.count > 0) {
                [self.dataArrayM addObjectsFromArray:dataArray];
                if (self.dataArrayM.count < self.tableViewPage*CountPerPage) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [self.tableView.mj_footer endRefreshing];
                }
                [self.tableView reloadData];
            } else {
                self.tableViewPage--;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        } else {
            self.tableViewPage--;
            [self.tableView.mj_footer endRefreshing];
            [MBProgressHUD showTipMessageInWindow:connectionError.localizedDescription];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArrayM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LSAttendanceManagementTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LSAttendanceManagementTableViewCellID forIndexPath:indexPath];
    cell.mainTitleLabel.text = [ValidateUtils validateNull:self.dataArrayM[indexPath.row][@"create_name"]];
    cell.mainSubtitleLabel.text = [ValidateUtils validateNull:self.dataArrayM[indexPath.row][@"approve_typename"]];
    cell.mainTimeLabel.text = [ValidateUtils validateNull:self.dataArrayM[indexPath.row][@"between_time"]];
    cell.mainStatusLabel.text = [ValidateUtils validateNull:self.dataArrayM[indexPath.row][@"status"]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return LSAttendanceManagementTableViewCellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    WS
    LSAttendanceRecordsDetailViewController *attendanceRecordsDC = [[LSAttendanceRecordsDetailViewController alloc] init];
    attendanceRecordsDC.refreshBlock = ^{
        [weakSelf loadData];
    };
    attendanceRecordsDC.fromVC = @"待办列表";
    attendanceRecordsDC.listID = [ValidateUtils validateNull:self.dataArrayM[indexPath.row][@"id"]];
    attendanceRecordsDC.approveType = [ValidateUtils validateNull:self.dataArrayM[indexPath.row][@"approve_type"]].integerValue;
    switch ([ValidateUtils validateNull:self.dataArrayM[indexPath.row][@"approve_type"]].integerValue) {
        case 1:
        {
            attendanceRecordsDC.tipTitleLabelArray = @[@"请假事由：",@"姓名：",@"职务：",@"科室：",@"请假类型：",@"请假时长：",@"时间：",@"抄送：",@"审批人："];
            attendanceRecordsDC.infoTitleLabelArray = @[@"APPROVE_MAIN",@"CREATE_NAME",@"TYPENAME",@"SYS_ORG_CODE",@"LEAVE_TYPE",@"LEAVE_DURATION",@"BETWEEN_TIME",@"SEND_NAME",@"APPROVE_NAME"];
        }
            break;
        case 2:
        {
            attendanceRecordsDC.tipTitleLabelArray = @[@"出差事由：",@"姓名：",@"职务：",@"科室：",@"出差地点：",@"会议培训期间费用支付单位：",@"出差时间：",@"出差时长：",@"乘坐交通工具（是否带车）：",@"抄送：",@"审批人：",@"备注："];
            attendanceRecordsDC.infoTitleLabelArray = @[@"APPROVE_MAIN",@"CREATE_NAME",@"TYPENAME",@"SYS_ORG_CODE",@"away",@"AWAY_MAIN",@"BETWEEN_TIME",@"AWAY_DURATION",@"AWAY_TOOL",@"SEND_NAME",@"APPROVE_NAME",@"AWAY_REMARKS"];
        }
            break;
        case 3:
        {
            attendanceRecordsDC.tipTitleLabelArray = @[@"用车事由：",@"姓名：",@"职务：",@"科室：",@"目的地：",@"用车人数：",@"车牌号：",@"出车公里数：",@"回车公里数：",@"用车公里数：",@"用车开始时间：",@"用车结束时间：",@"抄送：",@"审批人："];//,@"审批意见："
            attendanceRecordsDC.infoTitleLabelArray = @[@"APPROVE_MAIN",@"CREATE_NAME",@"TYPENAME",@"SYS_ORG_CODE",@"CAR_OPTION",@"CAR_NUM",@"CAR_NUMBER",@"MILEAGE_OUT",@"RETURN_MILEAGE",@"USE_MILEAGE",@"START_TIME",@"END_TIME",@"SEND_NAME",@"APPROVE_NAME"];//,@""
        }
            break;
        case 4:
        {
            [MBProgressHUD showTipMessageInWindow:@"记录已撤销"];
            return;
        }
            break;
        default:
        {
            [MBProgressHUD showTipMessageInWindow:@"状态错误"];
            return;
        }
            break;
    }
    if ([[ValidateUtils validateNull:self.dataArrayM[indexPath.row][@"status"]] isEqualToString:@"同意"]) {
        attendanceRecordsDC.approveState = 2;
    } else if ([[ValidateUtils validateNull:self.dataArrayM[indexPath.row][@"status"]] isEqualToString:@"不同意"]) {
        attendanceRecordsDC.approveState = 3;
    } else if ([[ValidateUtils validateNull:self.dataArrayM[indexPath.row][@"status"]] isEqualToString:@"待审批"]) {
        attendanceRecordsDC.approveState = 1;
    } else if ([[ValidateUtils validateNull:self.dataArrayM[indexPath.row][@"status"]] isEqualToString:@"已撤销"]) {
        attendanceRecordsDC.approveState = 4;
    }
    [self.navigationController pushViewController:attendanceRecordsDC animated:true];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaTopHeight)];
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.separatorColor = hexColor(DDDDDD);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = hexColor(EEEEEE);
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:LSAttendanceManagementTableViewCell.class forCellReuseIdentifier:LSAttendanceManagementTableViewCellID];
    }
    return _tableView;
}

- (NSMutableArray<NSDictionary *> *)dataArrayM {
    if (!_dataArrayM) {
        _dataArrayM = [[NSMutableArray alloc] init];
    }
    return _dataArrayM;
}

- (NZKNoDataView *)nodataView {
    if (!_nodataView) {
        _nodataView = [[NZKNoDataView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaTopHeight) withTip:@"暂无相关信息" andButtonText:nil];
    }
    return _nodataView;
}

- (NZKNetworkErrorView *)networkErrorView {
    if (!_networkErrorView) {
        WS
        _networkErrorView = [[NZKNetworkErrorView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaTopHeight) withRefreshBlock:^{
            [weakSelf loadData];
        }];
    }
    return _networkErrorView;
}

@end
